<div id="tb<?php echo $uniqid;?>" style="padding:5px">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:void(0);" onclick="wechatMenuManageAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="wechatMenuManageReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con"  style="-moz-user-select:none;">
    <table id="wechatMenuManage"></table>
</div>
<div id="wechatMenuManageDlg" class="easyui-dialog" title="信息框" closed="true"></div>
<script>
    var wechatMenuManageAdd = function () {
        $("#wechatMenuManageDlg").dialog({
            title: '新增菜单',
            closed: false,
            width: 900,
            height: 720,
            cache: false,
            href: '/admin/wechatmenu/add',
            modal: true,
            buttons: [{
                text: '保存',
                id: 'wechatMenuEditsubmit',
                iconCls: 'icon-ok',
            }, {
                text: '关闭',
                iconCls: 'icon-no',
                handler: function () {
                    $("#wechatMenuManageDlg").dialog('close');
                }
            }]
        });
    };
    var wechatMenuManageEdit = function (id) {
        row = $('#wechatMenuManage').datagrid('getSelected');
        if('undefined' === typeof id && row == null){
            $.messager.alert('提示','请先选择要编辑的行！','warning');
            return false;
        }
        if(row.is_publish){
            $.messager.alert('提示','发布中的菜单不能修改','warning');
            return false;
        }
        let row_id =  ('undefined' !== typeof id) ? id : row.id;
        if (row_id) {
            $("#wechatMenuManageDlg").dialog({
                title: '编辑菜单',
                closed: false,
                width: 900,
                height: 720,
                cache: false,
                href: '/admin/wechatmenu/edit?id=' + row_id,
                modal: true,
                buttons: [{
                    text: '保存',
                    id: 'wechatMenuEditsubmit',
                    iconCls: 'icon-ok',
                }, {
                    text: '关闭',
                    iconCls: 'icon-no',
                    handler: function () {
                        $("#wechatMenuManageDlg").dialog('close');
                    }
                }]
            });
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
    };
    var wechatMenuManageDelete = function (id) {
        if('undefined' === typeof id && $('#wechatMenuManage').datagrid('getSelected') == null){
            $.messager.alert('提示','请先选择要编辑的行！','warning');
            return false;
        }
        let row_id =  ('undefined' !== typeof id) ? id : $('#wechatMenuManage').datagrid('getSelected').id;
        if (row_id) {
            $.messager.confirm('提示', '确定删除吗？', function (r) {
                if (r) {
                    $.messager.progress();
                    $.post('/admin/wechatmenu/delete', {id: row_id}, function (data) {
                        $.messager.progress('close');
                        if (data.ret == 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            wechatMenuManageload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.alert('提示', '请先选择要删除的行！', 'warning');
        }
    };
    var wechatMenuManageCopy = function (rowId) {
        if (rowId) {
            $.messager.confirm('提示', '确定复制一个菜单吗？', function (r) {
                if (r) {
                    $.messager.progress();
                    $.post('/admin/wechatmenu/copymenu', {id: rowId}, function (data) {
                        $.messager.progress('close');
                        if (data.ret == 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            wechatMenuManageload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        }
    };
    var wechatMenuPublish = function (rowId) {
        if (rowId) {
            $.messager.confirm('提示', '确定去发布吗？', function (r) {
                if (r) {
                    $.messager.progress();
                    $.post('/admin/wechatmenu/publish', {id: rowId}, function (data) {
                        $.messager.progress('close');
                        if (data.ret == 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            wechatMenuManageload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        }
    };
    var wechatMenuManageReload = function () {
        $("#wechatMenuManage").datagrid('reload');
    };
    var wechatMenuManageload = function () {
        $('#wechatMenuManageKeywords').val('');
        $("#wechatMenuManage").datagrid('load', {keywords:''});
    };
    var wechatMenuManageSearch = function () {
        var height = $(window).height() - $(".top").height() - 46;
        var width = $(window).width() - $(".leftmenu").width() - 8;
        $("#wechatMenuManage").datagrid({
            title: '微信公众号菜单列表',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: false,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 10,
            pageList: [10, 15, 20, 25],
            method: 'post',
            sortName: 'created_at',
            sortOrder: 'desc',
            idField: 'id',
            treeField: 'title',
            url: '/admin/wechatmenu/index',
            queryParams: {},
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: false,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            columns: [[
                {field: 'id', title: 'ID', align: "center", halign: 'center',  width: fixWidth(5)},
                {field: 'name', title: '名称', align: "left", halign: 'center', width: fixWidth(25)},
                {field: 'content', title: '内容', align: "left", halign: 'center', width: fixWidth(40), formatter(value, rowData, rowIndex){
                    let content = '<div class="table">';
                    if(value){
                        for (let i = 0; i < value.length; i++){
                            content += '<span class="l-btn l-btn-small l-btn-left color9 table"><span class="l-btn-text">'+value[i].name+'</span></span>';
                        }
                    }
                    return content+'</div>';
                }},
                {field: 'is_publish', title: '发布状态', align: "center", halign: 'center', width: fixWidth(10), formatter(value, rowData, rowIndex){
                        return value===1 ? '<span style="color: #42af01;">已发布</span>' : '<span style="color: #414040;">未发布</span>';
                    }},
                {field: 'created_at', title: '创建时间', align: "center", halign: 'center', width: fixWidth(20)},
                {field: 'options', title: '操作', width: fixWidth(20), align: "center", halign: 'center', formatter: function (value, rowData, rowIndex) {
                        let buttons = '<div class="table">';
                        buttons += '<span class="l-btn l-btn-small l-btn-left color9 table" onclick="wechatMenuManageCopy(\'' + rowData.id + '\')"><span class="l-btn-text">复制</span></span>';
                        if(rowData.is_publish == 0){
                            buttons += '<span class="l-btn l-btn-small l-btn-left color9 table" onclick="wechatMenuPublish(\'' + rowData.id + '\')"><span class="l-btn-text" style="color: #0eb913;">去发布</span></span>';
                            buttons += '<span class="l-btn l-btn-small l-btn-left color9 table" onclick="wechatMenuManageEdit(\'' + rowData.id + '\')"><span class="l-btn-text">编辑</span></span>';
                            buttons += '<span class="l-btn l-btn-small l-btn-left color9 table" onclick="wechatMenuManageDelete(\'' + rowData.id + '\')"><span class="l-btn-text">删除</span></span>';
                        }
                        return buttons + '</div>';
                    }},
            ]],
            onDblClickRow: function (index, field) {
                wechatMenuManageEdit();
            }
        });
    };
    onDatagridResize("#wechatMenuManage");
    wechatMenuManageSearch();
</script>
